package com.dhecp.project.quartz.task;

import com.alibaba.fastjson.JSON;
import com.dhecp.core.utils.DateUtils;
import com.dhecp.project.api.domain.ApiResponse;
import com.dhecp.project.api.domain.FbaBox;
import com.dhecp.project.api.domain.FbaLogsticsOrderDetail;
import com.dhecp.project.api.domain.FbaWarehousing;
import com.dhecp.project.api.mapper.FbaBoxMapper;
import com.dhecp.project.api.mapper.FbaLogsticsOrderDetailMapper;
import com.dhecp.project.api.mapper.FbaWarehousingMapper;
import com.dhecp.project.api.service.ApiAmazonService;
import com.dhecp.project.api.service.ApiFbaInboundService;
import com.dhecp.project.api.service.ApiLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName ApiFbaInboundTask
 * @Description 头程线上定时器
 * @Author fxz
 * @Date 2023-04-28 14:31
 */
@Component("apiFbaInboundTask")
public class ApiFbaInboundTask {

    private static final Logger logger = LoggerFactory.getLogger(ApiFbaInboundTask.class);
    //亚马逊系统名称
    private static final String AMAZON_SYSTEM_NAME = "Amazon";

    @Autowired
    private ApiLogService apiLogService;

    @Autowired
    private ApiFbaInboundService apiFbaInboundService;

    @Autowired
    private FbaLogsticsOrderDetailMapper fbaLogsticsOrderDetailMapper;

    @Autowired
    private FbaBoxMapper fbaBoxMapper;

    @Autowired
    private FbaWarehousingMapper fbaWarehousingMapper;


    /**
     * 定时修改头程线上发货状态(24:00运行)
     */
    public void updateFbaInbound()
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("isTracking","否");
        params.put("stateTrack","4");
        params.put("state","1");
        params.put("creationMethod","自动");
        List<FbaBox> fbaBoxList = null;
        List<String> shipmentIdList = new ArrayList<>();
        List<FbaLogsticsOrderDetail> fbaLogsticsOrderDetailListNew = null;
        List<FbaLogsticsOrderDetail> fbaLogsticsOrderDetailList;
        ApiResponse apiResponse = new ApiResponse();
        try {
            String type = "UpdateFbaInbound";
            fbaBoxList = fbaBoxMapper.select(params);
            for (FbaBox fbaBox : fbaBoxList){
                shipmentIdList.add(fbaBox.getShipmentId());
            }
            if (shipmentIdList.size() != 0){
                fbaLogsticsOrderDetailList = fbaLogsticsOrderDetailMapper.queryFirstByShipmentIdList(shipmentIdList);
                for (FbaBox fbaBox : fbaBoxList){
                    int insertLogId = apiLogService.addApiLog(AMAZON_SYSTEM_NAME,type,"定时修改头程线上发货状态", "",0, "");
                    apiResponse = apiFbaInboundService.updateFbaInbound(fbaBox, fbaLogsticsOrderDetailListNew, fbaLogsticsOrderDetailList, apiResponse,params);
                    if (apiResponse.getCode() == 400){
                        apiLogService.updateApiLog(insertLogId, AMAZON_SYSTEM_NAME, type, "定时修改头程线上发货状态", "定时修改头程线上发货状态出错，错误为："+apiResponse.getMessage(), apiResponse.getCode(), apiResponse.getMessage());
                    }
                }
            }
        }catch (Exception ex){
            logger.error("定时修改头程线上发货状态出错，错误信息为：" + ex.getMessage());
        }
    }

    /**
     * 定时修改入仓单状态以及数量(23:00运行)
     */
    public void getTransportDetails ()
    {
        ApiResponse apiResponse = new ApiResponse();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("stateName","已完成");
        List<FbaWarehousing> fbaWarehousingList = fbaWarehousingMapper.select(params);
        for (FbaWarehousing fbaWarehousing : fbaWarehousingList){
            apiResponse = apiFbaInboundService.getTransportDetails(fbaWarehousing,apiResponse);
            if (apiResponse.getCode() == 400){
                apiLogService.addApiLog(AMAZON_SYSTEM_NAME,"getTransportDetails","定时修改入仓单状态以及数量", JSON.toJSONString(fbaWarehousing),apiResponse.getCode(), apiResponse.getMessage());
            }
        }
    }


    /**
     * 测试
     */
    public void getCs (){
        apiFbaInboundService.getCs();
    }


}
